package com.testjpa.businesslogic;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.testjpa.business.IProjectRemote;
import com.testjpa.entities.Project;

/**
 * Session Bean implementation class ProjectBean
 */
@Stateless
public class ProjectBean implements IProjectRemote {

	@PersistenceContext(unitName = "JPADB")
	private EntityManager entityManager;
	
    /**
     * Default constructor. 
     */
    public ProjectBean() {
    	
    }

	@Override
	public void saveProject(Project project) {
		entityManager.persist(project);
		System.out.println("---- projectPersisted: " + project);
	}

	@Override
	public Project findProject(Project project) {
		Project projectFinded = entityManager.find(Project.class, project.getId());
		System.out.println("---- projectFinded: " + projectFinded);
		return projectFinded;
	}

	@Override
	public List<Project> retrieveAllProjects() {
		String q = "SELECT p from " + Project.class.getName() + " p";
        Query query = entityManager.createQuery(q);
        List<Project> projects = query.getResultList();
        System.out.println("---- projects: " + projects);
		return projects;
	}

}
